package com.ludi.basics.controller;

import com.ludi.basics.constants.Constants;
import com.ludi.basics.pojo.vo.RespCodeType;
import com.ludi.basics.pojo.vo.RespResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

/**
 * @author ludi
 * @date:2020/8/21 17:52
 * @description:文件操作业务
 */
@Controller
@Api(tags = "文件操作")
public class FileController {
    //文件上传功能
    @ResponseBody
    @PostMapping(value = "/upload", consumes = "multipart/*",
            headers = "content-type=multipart/form-data")
    @ApiOperation(value = "文件上传")
    public RespResult upload(MultipartFile file){
        if(file==null){
            throw new RuntimeException("文件上传失败");
        }
        String filePath = "D://newTemp/test/"+file.getOriginalFilename();
        System.out.println("url:http://localhost:8080/test/"+file.getOriginalFilename());
        try {
            file.transferTo(new File(filePath));
        } catch (IOException e) {
            throw new RuntimeException("文件上传失败");
        }
        RespResult respResult = new RespResult();
        return respResult;
    }

}
